// 早餐食谱数据库
const breakfastRecipes = [
    {
        id: 1,
        name: "香蕉燕麦粥",
        time: 5,
        category: "中式",
        ingredients: ["香蕉1根", "燕麦片50g", "牛奶200ml", "蜂蜜适量"],
        steps: "1. 香蕉压成泥\n2. 加入燕麦片和牛奶\n3. 微波炉加热2分钟\n4. 加入蜂蜜调味",
        nutrition: "富含膳食纤维和钾，低GI值"
    },
    {
        id: 2,
        name: "鸡蛋三明治",
        time: 10,
        category: "西式",
        ingredients: ["全麦面包2片", "鸡蛋1个", "生菜2片", "番茄酱适量"],
        steps: "1. 鸡蛋煮熟切片\n2. 面包烤至微黄\n3. 铺上生菜和鸡蛋\n4. 加入番茄酱对切",
        nutrition: "优质蛋白质来源，饱腹感强"
    },
    {
        id: 3,
        name: "酸奶水果杯",
        time: 5,
        category: "饮品",
        ingredients: ["希腊酸奶150g", "蓝莓50g", "草莓3颗", "坚果碎适量"],
        steps: "1. 酸奶倒入杯中\n2. 加入切好的水果\n3. 撒上坚果碎\n4. 冷藏后食用更佳",
        nutrition: "富含益生菌和抗氧化物质"
    },
    {
        id: 4,
        name: "豆浆燕麦饮",
        time: 3,
        category: "饮品",
        ingredients: ["即食燕麦30g", "无糖豆浆200ml", "奇亚籽5g", "肉桂粉少许"],
        steps: "1. 所有材料混合\n2. 搅拌均匀\n3. 静置2分钟即可",
        nutrition: "植物蛋白丰富，促进肠道健康"
    },
    {
        id: 5,
        name: "蔬菜鸡蛋饼",
        time: 15,
        category: "中式",
        ingredients: ["鸡蛋2个", "胡萝卜丝30g", "葱花适量", "全麦面粉20g"],
        steps: "1. 所有材料混合成面糊\n2. 平底锅少油煎制\n3. 两面金黄即可出锅",
        nutrition: "维生素丰富，低脂健康"
    },
    {
        id: 6,
        name: "牛油果吐司",
        time: 8,
        category: "西式",
        ingredients: ["全麦吐司1片", "牛油果半个", "柠檬汁几滴", "黑胡椒适量"],
        steps: "1. 吐司烤至酥脆\n2. 牛油果压成泥\n3. 加入柠檬汁和胡椒\n4. 涂抹在吐司上",
        nutrition: "健康脂肪，美容养颜"
    }
];

// 食材营养知识分享
const nutritionKnowledge = [
    "🥚 鸡蛋：优质蛋白质来源，富含胆碱，有助于大脑健康",
    "🥛 牛奶：钙质丰富，有助于骨骼健康，还含有维生素D",
    "🌾 燕麦：富含膳食纤维，有助于降低胆固醇，稳定血糖",
    "🥑 牛油果：健康脂肪来源，富含单不饱和脂肪酸和维生素E",
    "🍌 香蕉：钾含量高，有助于维持血压稳定，提供快速能量",
    "🥦 西兰花：维生素C和K的极佳来源，富含抗氧化物质",
    "🐟 三文鱼：Omega-3脂肪酸丰富，有益心脏健康和大脑功能",
    "🌰 坚果：富含健康脂肪、蛋白质和微量元素，适量食用有益健康",
    "🍓 浆果类：抗氧化物质丰富，有助于抗衰老和改善记忆力",
    "🍠 红薯：富含β-胡萝卜素和膳食纤维，是优质碳水化合物来源"
];

// 初始化函数
function initApp() {
    displayRecipes(breakfastRecipes);
    displayNutritionKnowledge();
    setupEventListeners();
}

// 显示食谱卡片
function displayRecipes(recipes) {
    const container = document.getElementById('recipesContainer');
    container.innerHTML = '';

    recipes.forEach(recipe => {
        const card = createRecipeCard(recipe);
        container.appendChild(card);
    });
}

// 创建食谱卡片
function createRecipeCard(recipe) {
    const card = document.createElement('div');
    card.className = 'recipe-card';
    
    card.innerHTML = `
        <h3>${recipe.name}</h3>
        <div>
            <span class="recipe-time">${recipe.time}分钟</span>
            <span class="recipe-category">${recipe.category}</span>
        </div>
        <div class="recipe-ingredients">
            <strong>食材：</strong>${recipe.ingredients.join('、')}
        </div>
        <div class="recipe-steps">
            <strong>步骤：</strong><br>${recipe.steps}
        </div>
        <div style="margin-top: 10px; color: #666;">
            <strong>营养：</strong>${recipe.nutrition}
        </div>
    `;

    return card;
}

// 显示食材营养知识
function displayNutritionKnowledge() {
    const tipElement = document.getElementById('nutritionTip');
    const randomKnowledge = nutritionKnowledge[Math.floor(Math.random() * nutritionKnowledge.length)];
    tipElement.textContent = randomKnowledge;
}

// 设置事件监听器
function setupEventListeners() {
    document.getElementById('timeFilter').addEventListener('change', filterRecipes);
    document.getElementById('categoryFilter').addEventListener('change', filterRecipes);
    document.getElementById('searchInput').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            searchRecipes();
        }
    });
}

// 筛选食谱
function filterRecipes() {
    const timeFilter = document.getElementById('timeFilter').value;
    const categoryFilter = document.getElementById('categoryFilter').value;

    let filteredRecipes = breakfastRecipes;

    if (timeFilter !== 'all') {
        filteredRecipes = filteredRecipes.filter(recipe => recipe.time <= parseInt(timeFilter));
    }

    if (categoryFilter !== 'all') {
        filteredRecipes = filteredRecipes.filter(recipe => recipe.category === categoryFilter);
    }

    displayRecipes(filteredRecipes);
}

// 搜索食谱
function searchRecipes() {
    const searchTerm = document.getElementById('searchInput').value.toLowerCase();
    
    if (searchTerm.trim() === '') {
        filterRecipes();
        return;
    }

    const filteredRecipes = breakfastRecipes.filter(recipe => 
        recipe.name.toLowerCase().includes(searchTerm) ||
        recipe.ingredients.some(ingredient => ingredient.toLowerCase().includes(searchTerm)) ||
        recipe.category.toLowerCase().includes(searchTerm)
    );

    displayRecipes(filteredRecipes);
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', initApp);